from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from django.urls import reverse
from django.contrib.auth.hashers import make_password,check_password
# from . import user_decorator
import myadmin
models = myadmin.models

# 商场主页面
def index(request):
    # 查询所有分类
    category = models.Category.objects.all()
    # 查询所有商品
    goods = models.Goods.objects.all()
    content = {'category': category, 'goods': goods}
    return render(request, 'myhome/index.html', content)

# 用户登录
def login(request):
    title = '用户登录'
    if request.method == "GET":
        uphone = request.COOKIES.get('uphone', '')
        content = {'title': title}
        return render(request, 'myhome/login.html',content)
    elif request.method == "POST":
        userinfo = request.POST.dict()
        uphone = userinfo['uphone']
        print("uphone", uphone)
        users = models.User.objects.filter(uphone=uphone)
        # 通过长度判断是否有这个对象
        print('users', users)
        if len(users) >= 1:
            # 密码加密
            for i in users:
                if i.ustatus != -1:
                    if check_password(userinfo['upwd'], i.upwd):
                        userinfo2 = models.User.objects.get(id=i.id)
                        # 用来判断有没有登录过
                        request.session['userinfo'] = {'uid': i.id, 'uname': i.uname, 'uphone': i.uphone}
                        print('request.seeeion1', request.session)
                        print('request.session:', request.session['userinfo'])
                        content = {"userinfo": userinfo2}
                        print('content', content)
                        return redirect(reverse('myhome_index'))
                    else:
                        return HttpResponse("<script>alert('登录失败,密码错误');location.href='" + reverse('myhome_login') + "'</script>")
        else:
            return HttpResponse("<script>alert('登录失败,用户名不存在');location.href='" + reverse('myhome_login') + "'</script>")

def logout(request):
    # 清空session信息
    request.session.flush()
    # 转向首页
    return redirect(reverse('myhome_index'))

def register(request):
    if request.method == 'GET':
        title = '用户注册'
        content = {'title': title}
        return render(request, 'myhome/register.html', content)
    elif request.method == 'POST':
        userinfo1 = request.POST.dict()
        print(userinfo1)
        if userinfo1['uphone'] == '' or userinfo1['upassword'] == '':
            return HttpResponse("<script>alert('你填写的信息不完整,请重新填写');location.href='" + reverse('myhome_register') + "'</script>")
        userinfo1.pop('csrfmiddlewaretoken')
        phones = userinfo1['uphone']
        userinfo = models.User.objects.filter(uphone = phones)
        flage = userinfo.count()
        if flage:
            for i in userinfo:
                if i.ustatus != -1:
                    return HttpResponse("<script>alert('手机号已经存在');history.back(-1)</script>")
            else:
                password = make_password(userinfo1['upassword'], None, 'pbkdf2_sha256')
                newusers = models.User()
                newusers.uphone = phones
                newusers.upwd = password
                newusers.save()  # 向数据库中存入数据
                return HttpResponse("<script>alert('注册成功,请登录');location.href='" + reverse('myhome_login') + "'</script>")
        else:
            password = make_password(userinfo1['upassword'], None, 'pbkdf2_sha256')
            newusers = models.User()
            newusers.uphone = phones
            newusers.upwd = password
            newusers.save()  # 向数据库中存入数据
            return HttpResponse("<script>alert('注册成功,请登录');location.href='" + reverse('myhome_login') + "'</script>")

